<?php
/*****************************************************
 函数名称：mail_notify v1.0 by willin kan. 
 函数作用：当勾选了回复选项之后，有人回复之后就会email通知
******************************************************/
function get_comment_notify($parent_id)
{
    global $wpdb;
  	$result = $wpdb->get_row("SELECT meta_value FROM {$wpdb->commentmeta} where comment_id = $parent_id and meta_key='mail_notify'");
    return $result->meta_value;
}

function comment_mail_notify($comment_id)
{
  $admin_notify = '1';                         // admin 要不要收回复通知 ( '1'=要 ; '0'=不要 )
  $admin_email = get_bloginfo ('admin_email'); // $admin_email 可改成你指定的 e-mail.
  $comment = get_comment($comment_id);
  $comment_author_email = trim($comment->comment_author_email);
  $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  global $wpdb;
  if (!(($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1')))
	 $wpdb->query("INSERT INTO {$wpdb->commentmeta}(comment_id,meta_key,meta_value) VALUES($comment_id,'mail_notify','0')"); 
  if($parent_id == '0')
     return;
  $spam_confirmed = $comment->comment_approved;
  if ($parent_id != '' && $spam_confirmed != 'spam' && get_comment_notify($parent_id) != '0') 
  {
    $wpdb->query("INSERT INTO {$wpdb->commentmeta}(comment_id,meta_key,meta_value) VALUES('1','enter','1')"); 
    $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail发出的email, no-reply 可改为可用的 e-mail.
	$parent_comment = get_comment($parent_id);
    $to = trim($parent_comment->comment_author_email);
    $subject = '您在 [' . get_option("blogname") . '] 的评论有了回应';
    $message = '
    <div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px; border-radius:5px;">
      <p>' . trim($parent_comment->comment_author) . '，您好!</p>
      <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
       . trim($parent_comment->comment_content) . '</p>
      <p>' . trim($comment->comment_author) . ' 给您的回应:<br />'
       . trim($comment->comment_content) . '<br /></p>
      <p>您可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回应的完整內容</a>，或者复制以下链接到浏览器：</p>
	  <p>' . htmlspecialchars(get_comment_link($parent_id)) . '</p>
      <p>感谢您对<a href="' . get_option('home') . '">' . get_option('blogname') . '</a>的关注。</p>
      <p>(此邮件由<a href="' . get_option('home') . '">' . get_option('blogname') . '</a>的邮件系统自动发出，请勿答复.)</p>
    </div>';
    $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
    $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
    wp_mail( $to, $subject, $message, $headers );
	admin_mail_notify($comment_id);
    //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
  }
}
add_action('comment_post', 'comment_mail_notify');

//通知博主有人发表评论
function admin_mail_notify($comment_id)
{
	$admin_email = get_bloginfo ('admin_email');
	$comment = get_comment($comment_id);
	$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
	$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail发出的email, no-reply 可改为可用的 e-mail.

	if($parent_id == '0')
	{
		$message = '<div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px; border-radius:5px;">'.
		trim($comment->comment_author) . ' 的留言:<br />'
			   . trim($comment->comment_content) . '<br /></p>
			  <p>您可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回应的完整內容</a>，或者复制以下链接到浏览器：</p>
			  <p>' . htmlspecialchars(get_comment_link($parent_id)) . '</p>
			</div>';	
	}
	else
	{
		$parent_comment = get_comment($parent_id);
		$message = '<div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px; border-radius:5px;">
		  <p>' . trim($parent_comment->comment_author) . '曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
		   . trim($parent_comment->comment_content) . '</p>
		  <p>' . trim($comment->comment_author) . ' 的回应:<br />'
		   . trim($comment->comment_content) . '<br /></p>
		  <p>您可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回应的完整內容</a>，或者复制以下链接到浏览器：</p>
		  <p>' . htmlspecialchars(get_comment_link($parent_id)) . '</p>
		</div>';
	}
	$subject = '您的博客 [' . get_option("blogname") . ']有了评论';	
    $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
    $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
	wp_mail( $admin_email, $subject, $message, $headers );
}

/* 添加到评论中 */
function add_checkbox_cmn() {
  echo '<input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked"/><label for="comment_mail_notify">回复我时邮件通知</label>';
}
add_action('comment_form', 'add_checkbox_cmn');
?>